<template>
  <div>
    <!-- 面包屑导航区域 -->
    <div class="bread_box">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <!-- 搜索与按钮区域 -->
      <el-form
        class="search_from"
        size="small"
        :inline="isInline"
        label-width="70px"
      >
        <el-form-item label="用户名称">
          <el-input
            placeholder="请输入用户名"
            clearable
            v-model="pageInfo.text"
            @clear="getUserList()"
          ></el-input>
        </el-form-item>

        <el-form-item label="状态">
          <el-select placeholder="账号状态">
            <el-option label="正常" value="true"></el-option>
            <el-option label="封号" value="false"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="创建时间">
          <el-date-picker
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="selectUser()"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini">重置</el-button>
        </el-form-item>
      </el-form>

      <div>
        <el-button type="primary" icon="el-icon-plus" size="mini"
          >新增</el-button
        >
        <el-button type="success" icon="el-icon-edit" size="mini"
          >修改</el-button
        >
        <el-button type="danger" icon="el-icon-delete" size="mini"
          >删除</el-button
        >
        <el-button type="warning" icon="el-icon-download" size="mini"
          >导出</el-button
        >
      </div>

      <!-- 用户列表区域 -->
      <el-table
        :data="userList"
        :header-cell-style="{ 'text-align': 'center' }"
        :cell-style="{ 'text-align': 'center' }"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <!-- <el-table-column label="序号" type="index" ></el-table-column> -->
        <el-table-column label="头像">
          <template slot-scope="scope">
            <!-- {{'http://120.25.169.204:8080' + scope.row.profilePhoto}} -->
            <el-avatar :size="60">
              <img
                :src="$store.state.user.backUrl + scope.row.profilePhoto"
                alt=""
              />
            </el-avatar>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column
          label="邮箱"
          prop="email"
          width="180px"
        ></el-table-column>
        <el-table-column label="性别">
          <template slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.sex">
              <i class="el-icon-male"></i>
            </el-tag>
            <el-tag type="danger" v-else>
              <i class="el-icon-female"></i>
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="身份等级">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.roleId === 1" type="success">
              <span>admin</span>
            </el-tag>
            <el-tag v-else type="info">
              <span>common</span>
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建日期" prop="created"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <!-- {{scope.row}} -->
            <el-switch
              v-model="scope.row.valid"
              @change="userValidChanged(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 查看详情按钮 -->
            <el-tooltip
              class="item"
              effect="dark"
              content="查看详细"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="text"
                icon="el-icon-view"
                size="mini"
              ></el-button>
            </el-tooltip>
            <!-- 删除按钮 -->
            <el-tooltip
              class="item"
              effect="dark"
              content="删除用户"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="text"
                icon="el-icon-delete"
                size="mini"
              ></el-button>
            </el-tooltip>
            <!-- 分配角色按钮 -->
            <el-tooltip
              class="item"
              effect="dark"
              content="分配角色"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="text"
                icon="el-icon-setting"
                size="mini"
                @click="toUpdateDialog(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.cur"
        :page-sizes="[4, 8, 10, 20]"
        :page-size="pageInfo.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>

    <!-- 修改橘色对话框 -->
    <el-dialog title="分配角色" :visible.sync="updateDialogVisible" width="30%">
      <el-form ref="updateFormRef" :model="updateForm" laidbel-width="100px">
        <el-row>
          <el-col :span="18" :offset="3">
            <el-form-item>
              <span>功能描述：1为管理员、2为普通用户</span>
            </el-form-item>
            <!-- <el-form-item label="用户名：" prop="id">
                  <el-input v-model="updateForm.id"></el-input>
                </el-form-item> -->
            <el-form-item label="角色描述：" prop="role">
              <el-select v-model="updateForm.role" placeholder="角色状态">
                <el-option label="管理员" value="1"></el-option>
                <el-option label="普通用户" value="2"></el-option>
              </el-select>
            </el-form-item>
            <!-- <el-form-item label="角色描述：" prop="name">
                  <el-input v-model="updateForm.name"></el-input>
                </el-form-item> -->
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateRole()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 获取用户列表的参数对象
      pageInfo: {
        cur: 1, // 当前页
        size: 4, // 页大小
        text: '' // 查询条件，可以为空
      },
      // 查询用户名参数
      queryData: {
        id: '',
        email: '',
        name: ''
      },
      // 用户列表
      userList: [],
      total: 0,
      isInline: true,
      // 控制显示隐藏分配权限对话框
      updateDialogVisible: false,
      // 分配权限表单
      updateForm: {
        id: '', // 用户id
        role: '' // 角色id
      }
    }
  },
  methods: {
    // 获取用户列表
    async getUserList () {
      const { data: res } = await this.$http.get('/user/page', {
        params: this.pageInfo
      })
      // console.log('用户列表', res)
      if (res.code === -10) {
        window.sessionStorage.clear()
        this.$router.push('/login')
        return this.$message.error('登陆过期，请重新登陆！')
      } else if (res.code !== 200) return this.$message.error('获取用户列表失败！')
      // 赋值
      this.userList = res.data.list
      this.total = res.data.total
    },
    // 监听 size 改变的事件
    handleSizeChange (newSize) {
      //   console.log(newSize)
      this.pageInfo.size = newSize
      this.getUserList()
    },
    // 监听 cur 页码值 改变的事件
    handleCurrentChange (newCur) {
      //   console.log(newCur)
      this.pageInfo.cur = newCur
      this.getUserList()
    },
    // 监听 switch 开关状态
    async userValidChanged (userInfo) {
      //   console.log(userInfo.id)
      //   console.log(userInfo.valid)
      const { data: res } = await this.$http.get(`/user/setValid/${userInfo.id}?flag=${userInfo.valid}`)
      //   const { data: res } = await this.$http.get('/user/setValid/', { id: userInfo.id, flag: userInfo.valid })
      //   console.log(res)
      if (res.code !== 200) {
        userInfo.valid = !userInfo.valid
        return this.$message.error('更新用户状态失败！')
      }
      return this.$message.success('更新用户状态成功！')
    },
    // 根据用户名参数，查询用户
    async selectUser () {
      const { data: res } = await this.$http.get('/user/page', {
        params: this.pageInfo
      })
      // console.log(res)
      if (res.code !== 200) return this.$message.error('查询用户失败！')
      this.userList = res.data.list
      this.total = res.data.total
    },
    // 打开分配权限对话框
    toUpdateDialog (info) {
      this.updateForm.id = info.id
      this.updateForm.role = info.roleId
      this.updateDialogVisible = true
    },
    // 更新用户权限角色
    async updateRole () {
      const { data: res } = await this.$http.get('/user/updateRole', { params: this.updateForm })
      if (res.code !== 200) return this.$message.error('角色分配失败！')
      this.$message.success('角色分配成功')
      this.getUserList()
      console.log('参数', this.updateForm)
      this.updateDialogVisible = false
    }
  },
  created () {
    this.getUserList()
  }
}
</script>

<style lang="less" scoped>
.el-avatar {
  // border: 1px solid rgb(243, 219, 190);
  background-color: rgb(252, 251, 249);
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 8px;
  position: relative;
  img {
    border-radius: 50%;
    width: 90%;
    height: 90%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }
}

.search_from {
  .el-form-item {
    min-width: 230px !important;
  }
}
</style>
